#ifndef K_EVENT_H
#define K_EVENT_H
#pragma once

#include "K_Windows.h"
#include "KGlobal.h"
#include "KNoCopy.h"

class KEvent : KNoCopy {
public:
    KEvent(BOOL manual, BOOL signaled)
        : m_event(KVerify(CreateEvent(NULL, manual, signaled, NULL))) {}
    virtual ~KEvent()               { CloseHandle(m_event); }
public:
    HANDLE handle() const           { return m_event; }
public:
    BOOL reset()                    { return ResetEvent(m_event); }
    BOOL set()                      { return SetEvent(m_event); }
    bool wait(DWORD ms = INFINITE)  { return WaitForSingleObject(m_event, ms) == WAIT_OBJECT_0; }
private:
    HANDLE m_event;
};

class KAutoEvent : public KEvent {
public:
    explicit KAutoEvent(BOOL signaled = FALSE) : KEvent(FALSE, signaled) {}
};

class KManualEvent : public KEvent {
public:
    explicit KManualEvent(BOOL signaled = FALSE) : KEvent(TRUE, signaled) {}
};

#endif // K_EVENT_H